package com.smart.servlet;

import com.smart.dao.DBupdate;
import com.smart.domain.StudentUser;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet( name = "updateServlet",urlPatterns ="/updateServlet")
/*获取表单提交的修改后的学生信息 */
public class updateServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        request.setCharacterEncoding("UTF-8"); //设置请求的字符编码格式，与界面的字符编码保持一致

        /*以下是获得表单提交过来的值 */
        String id = request.getParameter("id");
        String grade = request.getParameter("grade");

        /* 以下代码将获得的表单值封装到用户信息对象中 */
        StudentUser user = new StudentUser();
        user.setId(id);
        user.setGrade(grade);
        boolean flag = DBupdate.getInstance().stuupdate(user);
        if(flag){
            request.getSession().setAttribute("id", user.getId());
            request.getRequestDispatcher("success.html").forward(request, response);
        }else{
            response.sendRedirect("error.html");
        }
    }

}